package com.cn.jinl.SystemLogInterceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.ISystemLogService;
import com.cn.jinl.domain.SystemLog;
import com.cn.jinl.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Aspect
@Component
@Order(2)
public class SystemLogInterceptor {
    @Autowired
    private ISystemLogService systemLogService;


    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("execution(public * com.cn.jinl.controller..*.*(..))&& !execution(public * com.cn.jinl.controller.SystemLogController.*(..))")
    public void SystemLog() {
    }



    @AfterReturning("SystemLog()")
    public void AfterReturning(JoinPoint jp) {
        String className = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        String moduleName = "";
        String methodNodule = "";
        if (StringUtils.isNotBlank(className) && StringUtils.isNotBlank(methodName)) {
            String[] split = className.split("[.]");
            String lastClassName = split[split.length - 1];
            for (ModuleLog.ModuleClass m : ModuleLog.ModuleClass.values()) {
                String nameEn = m.toString();
                if (StringUtils.equals(lastClassName, nameEn)) {
                    moduleName = m.getNameCn();
                }
            }

            for (ModuleLog.ModuleMethod m : ModuleLog.ModuleMethod.values()) {
                String nameEn = m.toString();
                if (StringUtils.equals(methodName, nameEn)) {
                    methodNodule = m.getNameCn();
                }
            }
            if (StringUtils.isNotBlank(moduleName) && StringUtils.isNotBlank(methodNodule)) {
                HttpSession httpSession = httpServletRequest.getSession();
                String value = (String) redisTemplate.opsForValue().get(httpSession.getId());
                if (StringUtils.isNotBlank(value)) {
                    try {
                        String decode = MD5Util.getDecoder(value);
                        JSONObject jsonObject = JSON.parseObject((String) decode);
                        String name = jsonObject.getString("userAccount");
                        String userId = jsonObject.getString("id");
                        SystemLog systemLog = new SystemLog();
                        systemLog.setDetail(methodNodule);
                        systemLog.setModuleName(moduleName);
                        systemLog.setUserId(userId);
                        systemLog.setUserName(name);
                        systemLogService.addSystemLog(systemLog);
                    } catch (Exception e) {
                    }

                }
            }
        }
    }

}
